package com.tianzhu.service;

import java.util.List;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tianzhu.domain.Cart;
import com.tianzhu.domain.Cartitem;
import com.tianzhu.domain.Customerorder;
import com.tianzhu.repositories.CartRepo;
import com.tianzhu.repositories.CustomerOrderRepo;

@Service
@Transactional
@Slf4j
public class CustomerOrderService {
	
	@Autowired
	CustomerOrderRepo customerOrderRepo;
	
	@Autowired
	CartRepo cartRepo;
	
	public void addCustomerOrder(Customerorder customerOrder){
		customerOrderRepo.save(customerOrder);
	}

    public double getCustomerOrderGrandTotal(Long cartId){
    	
    	double grandTotal = 0;
    	
    	Cart cart = cartRepo.findOne(cartId);
        List<Cartitem> cartItems = cart.getCartItems();

        for (Cartitem item : cartItems){
            grandTotal += item.getTotalprice();
        }

        return grandTotal;
    	
    }

}
